//
//  sceneContentView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/6/17.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "sceneContentView.h"
#import "addSceneContentView.h"

@interface sceneContentView () <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *fundationArray;
    addSceneContentView *fundationView;
}

@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, strong) UITableView *myTableView;

@end

@implementation sceneContentView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame andFundationArray:(NSArray *)array
{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = BACKGROUND_COLOR;
        fundationArray = [NSMutableArray arrayWithArray:array];
        if(!fundationArray){
            fundationArray = [[NSMutableArray alloc] init];
        }
        [self addSubview:self.startButton];
        [self addSubview:self.myTableView];
    }
    return self;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [fundationArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *sceneTableCellIdentify = @"sceneCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sceneTableCellIdentify];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sceneTableCellIdentify];
    }
    for (UIView *view in cell.subviews) {
        if([view isKindOfClass:[UIImageView class]] || [view isKindOfClass:[UIButton class]]){
            [view removeFromSuperview];
        }
    }
    UIImageView *imageView;
    cell.textLabel.text = [NSString stringWithString:[[fundationArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
    NSString *deviceType = [[fundationArray objectAtIndex:indexPath.row] objectForKey:@"type"];
    UIImage *image;
    if([deviceType isEqualToString:AIR_TYPE]){
        image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_airconditioning");
    }else if([deviceType isEqualToString:TV_TYPE]){
        image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_tv");
    }else if([deviceType isEqualToString:LIGHT_TYPE1] || [deviceType isEqualToString:LIGHT_TYPE2] || [deviceType isEqualToString:LIGHT_TYPE3]){
        image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_light");
        
    }else if([deviceType isEqualToString:CURTAIN_TYPE] || [deviceType isEqualToString:MB_TYPE]){
        image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_curtains");
    }else if([deviceType isEqualToString:STB_TYPE]){
        image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_stb");
    }else if([deviceType isEqualToString:DVD_TYPE]){
        image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_dvd");
    }else if([deviceType isEqualToString:AMP_TYPE]){
        image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_amp");
    }else{
        image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_lang");
    }
    imageView = [[UIImageView alloc] initWithImage:image];
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - 50, 10, 30, 30)];
    NSString *fundationName = [[fundationArray objectAtIndex:indexPath.row] objectForKey:@"fundationName"];
    if([fundationName isEqualToString:@"开"]){
        [rightButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnon") forState:UIControlStateNormal];
    }else{
        [rightButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnoff") forState:UIControlStateNormal];
    }
    [cell addSubview:rightButton];
    
    [imageView setFrame:CGRectMake(self.bounds.size.width * 0.4, 10, 30, 30)];
    [cell addSubview:imageView];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - method
- (void)addFundation
{
    fundationView = [[addSceneContentView alloc] initWithFrame:self.bounds andJsonDic:self.jsonDic];
    __weak sceneContentView *s = self;
    fundationView.getMyResult = ^(NSDictionary *dic){
        [s getNewFundation:dic];
    };
    [self addSubview:fundationView];
}

-(void)hideFundationView
{
    [fundationView removeFromSuperview];
    fundationView = nil;
}

- (void)getNewFundation:(NSDictionary *)dic
{
    [fundationArray addObject:dic];
    [self.myTableView reloadData];
    self.getAllFunction(fundationArray);
}

#pragma mark - response
- (void)startScene
{
    for (NSDictionary *dic in fundationArray) {
        NSString *str = [dic objectForKey:@"message"];
        self.sendFundationMessage(str);
        [NSThread sleepForTimeInterval:1.0];
    }
    
}

#pragma mark - getter setter
- (UIButton *)startButton
{
    if(!_startButton){
        _startButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 35)];
        _startButton.center = CGPointMake(self.bounds.size.width / 2, 30);
        [_startButton setTitle:@"执行场景" forState:UIControlStateNormal];
        [_startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _startButton.backgroundColor = TEXT_GREEN_COLOR;
        _startButton.layer.cornerRadius = 5.0;
        [_startButton addTarget:self action:@selector(startScene) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startButton;
}

- (UITableView *)myTableView
{
    if(!_myTableView){
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, self.bounds.size.width, self.bounds.size.height - 40) style:UITableViewStylePlain];
        UIView *footView = [[UIView alloc] init];
        _myTableView.tableFooterView = footView;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}

@end
